package dbzx.pdf;
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26628678/manipulating-witdh-and-height-of-rows-and-cells-in-itextsharp
 */

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class PdfTableAndCellStyle {
	/** windows字体路径 */
	public static final String FONT = "C:/WINDOWS/Fonts/simhei.ttf";
    public static final String DEST = "E:/项目资料/pdfCreat/";
    public static void main(String[] args) throws IOException, DocumentException {
    	String destPath = DEST + System.currentTimeMillis() + ".pdf";
        File file = new File(destPath);
        file.getParentFile().mkdirs();
        new PdfTableAndCellStyle().createPdf(destPath);
        System.out.println("pdf生成！！");
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
    	Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 13);
    	
        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        float[] columnWidths = {1, 5, 5};
        PdfPTable table = new PdfPTable(columnWidths);
        table.setWidthPercentage(100);//表格占页面宽度百分比
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
        font .setColor(GrayColor.GRAYWHITE);
        PdfPCell cell = new PdfPCell(new Phrase("This is a header哈哈哈哈", font));
        cell.setBackgroundColor(GrayColor.GRAYBLACK);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(3);
        table.addCell(cell);
        table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table.addCell("#");
            table.addCell("Key");
            table.addCell("Value");
        }
        /** 设置表格页眉行数和页脚行数，只用当表格内容溢出本页时生效，相互配合。
         * 0计算规则：表格正式生成后，取header的值减一行，作为所有的header和footer行，并且第二行重复。所以，这两个参数的值只能是3和1.
         * 	 header为3，footer为1，结果就是，每页表格前两行为header行，最后一行为footer行。
         */
        table.setHeaderRows(1);
//        table.setFooterRows(1);
        table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        for (int counter = 1; counter < 101; counter++) {
            table.addCell(String.valueOf(counter));
            table.addCell("key " + counter);
            table.addCell("value " + counter);
        }
        document.add(table);
        document.close();
    }
}